package com.mzy.linear_struct.ks.回溯;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: mizy3
 * @Date: 2024/07/26/上午11:23
 * @Description:
 */
public class Pro39组合总和 {

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        Deque<Integer> path = new LinkedList<>();

        dfs(candidates, 0, candidates.length, target, path, res);

        System.out.println(res);


        return null;
    }

    private void dfs(int[] candidates, int begin, int len, int target, Deque<Integer> path,
                     List<List<Integer>> res) {
        if (target < 0) {
            return;
        }
        if (target == 0) {
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = begin; i < len; i++) {
            path.addLast(candidates[i]);

            dfs(candidates, i, len, target - candidates[i], path, res);

            path.removeLast();
        }

    }

    public static void main(String[] args) {
        new Pro39组合总和().combinationSum(new int[]{2, 3, 6, 7}, 7);
    }
}
